<?php

namespace App\Models\Seckill;

use App\Models\Goods\CmcGood;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Request;

class CmcStoreSeckill extends Model
{
	use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'cmc_store_seckill';


    public function SeckillGoods()
    {
        return $this->hasMany(CmcGood::class);
    }

    protected function execute(Request $request)
    {
        $data =CmcStoreSeckill::all()->toArray();
        // 获取当前时间
        $time=time();
        foreach ($data as $k=>$v){
            // 秒杀状态 ： 已结束
            if ($v['status'] == 0){
                // 与结束时间对比
                if ($time > strtotime($v['end_time'])){
                    // 修改秒杀商品状态
                    $result = CmcStoreSeckill::where('id',$v['id'])->update(['status'=>1]);
                    // 添加日志
                    if ($result){
                        Log::record('秒杀商品表中id=' . $v['id'] . '该秒杀商品已经结束，状态修改为关闭');
                    }
                }
            }
        }
    }
}
